package com.briup.sxau.web.controller;

import com.briup.sxau.bean.Student;
import com.briup.sxau.service.IStudentService;
import com.briup.sxau.util.Result;
import com.briup.sxau.util.SxauException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
//url  前缀  student  而且要支持各种请求方式
@RequestMapping("student")
@Api(tags = "学生管理模块")
//@Api(tags = "学生管理模块") 对当前controller描述
public class StudentController {
    @Autowired
    IStudentService studentService;

    @ApiOperation("查询全部的学生信息带分页")
    @GetMapping("findAll")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page",value = "当前页码必须大于0"),
            @ApiImplicitParam(name = "pageSize",value = "页面展示数据条数")
    })
    public Result findAll(int page, int pageSize) {
        Page<Student> students = studentService.findAll(page, pageSize);
        return Result.success(students);
    }

    /**
     * 通过id获取学生信息
     *
     * @return
     * @PathVariable 从url获取变量
     */
    @GetMapping("{id}")
    public Result findStudent(@PathVariable int id) {


            Student student = studentService.findById(id);
            return Result.success(student);

    }

    /**
     * 通过id删除学生信息
     * @param id 学生id
     * @return
     */
    @DeleteMapping("{no}")
    public Result deleteById(@PathVariable(name = "no") int id){
        studentService.deleteById(id);
        return  Result.success();
    }
//    新增学生信息
    @ApiOperation("新增学生信息 id没有设置主键自增 要手动填入")
    @PostMapping("save")
    public Result save(@RequestBody Student student){

            studentService.save(student);
            return Result.success();


    }
}
//url  GET:127.0.0.1:9999/student/1
//url  DELETE:127.0.0.1:9999/student/1